﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Lapis.ModuleA.Views
{
    /// <summary>
    /// PageOne.xaml 的交互逻辑
    /// </summary>
    public partial class PageOne : UserControl
    {
        public PageOne()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        public string DoWork(string str)
        {
            Thread.Sleep(3000); //模拟延迟 比如网络请求 用了3000毫秒
            return "输出：" + str;
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        public Task<string> DoWorkByTask(string str)
        {
            return Task.Run(() => { Thread.Sleep(3000); return "输出：" + str; });
            //return Task.Run(() =>
            //{
            //    Thread.Sleep(3000); //模拟延迟 比如网络请求 用了3000毫秒
            //    return "输出：" + str;
            //});
        }

        private async void button1_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch watch = Stopwatch.StartNew();
            var t1 = DoWorkByTask("测试值1");

            var t2 = DoWorkByTask("测试值2");
            
            string r1 = await t1;

            textBox1.AppendText(r1 + "\r\n");

            string r2 = await t2;

            textBox1.AppendText(r2 + "\r\n");

            watch.Stop();

            textBox1.AppendText("耗时：" + watch.ElapsedMilliseconds / 1000 + "\r\n");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() => //如果不加Task.Run，界面会卡
            {
                Stopwatch watch = Stopwatch.StartNew();
                Task<string> t1 = Task.Run<string>(() => { return DoWork("测试值1"); });
                Task<string> t2 = Task.Run<string>(() => { return DoWork("测试值2"); });

                Task.WaitAll(t1, t2);
                watch.Stop();
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => //线程中修改控件数据要使用Invoke
                {
                    textBox1.AppendText(t1.Result + "\r\n");
                    textBox1.AppendText(t2.Result + "\r\n");
                    textBox1.AppendText("耗时：" + watch.ElapsedMilliseconds / 1000 + "\r\n");
                }));
                

            });
        }

        private async void button3_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch watch = Stopwatch.StartNew();
            var r1 = await DoWorkByTask("测试值1");
            var r2 = await DoWorkByTask("测试值2");

            textBox1.AppendText(r1 + "\r\n");

            textBox1.AppendText(r2 + "\r\n");

            watch.Stop();
            textBox1.AppendText("耗时：" + watch.ElapsedMilliseconds / 1000 + "\r\n");
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() => //如果不加Task.Run，界面会卡
            {
                Stopwatch watch = Stopwatch.StartNew();

                Task<string> t1 = Task.Run<string>(() => { return DoWork("测试值1"); });
                Task<string> t2 = Task.Run<string>(() => { return DoWork("测试值2"); });
                t1.Wait();
                this.Dispatcher.BeginInvoke(new Action(() => //线程中修改控件数据要使用Invoke
                {
                    textBox1.AppendText(t1.Result + "\r\n");
                }));

                t2.Wait();
                this.Dispatcher.BeginInvoke(new Action(() => //线程中修改控件数据要使用Invoke
                {
                    textBox1.AppendText(t2.Result + "\r\n");
                }));

                watch.Stop();
                this.Dispatcher.BeginInvoke(new Action(() => //线程中修改控件数据要使用Invoke
                {
                    textBox1.AppendText("耗时：" + watch.ElapsedMilliseconds / 1000 + "\r\n");
                }));
            });
        }
    }
}
